/**
 *  @file HttpEndPoint.h
 */
#ifndef _HTTPENDPOINT_H_
#define _HTTPENDPOINT_H_

#include "../../common/BaseObject.h"
#include "../../common/ObjPtrT.h"
#include "../../collections/ListT.h"
#include "HttpParams.h"
#include "HttpMessage.h"

namespace cppflib
{

class CFString;

namespace collections { class BinBuf; }
namespace io { class Stream; }
namespace security { namespace cert { class X509Certificate; } }

namespace net
{

class Socket;
class ICancelContext;

namespace http
{

/**
 *  Base class for HttpClient and HttpServerEndPoint
 */
class _DLLAPI HttpEndPoint : public BaseObject
{
private:
    collections::BinBuf *pTempBuf;
    Socket *pSocket;  

    void ReleaseBeforeThrowing();

protected:
    HttpParams * pParams;

    PUBLIC bool __GetHttpVersion(const CFString &line, CFString &ver, int &endOfVer);
    PUBLIC const sz_t STREAMBUFFERSIZE;

    HttpEndPoint(void);
    HttpEndPoint(HttpParams * pParams);

    bool IsServerSide();
    bool IsSocketValid();
    bool IsSSLSocket();
    void RemoveSocket(bool isClose);
    void AssignSocket(Socket *pNewSocket);
    Socket & GetSock();

    bool GetOneLine(CFString &line);
    
    bool SendString(Socket &sock, CFString &srcStr);
    void SendHeaders(HttpMessage &httpMsg);
    void SendBody(HttpMessage &httpMsg);
    void SendBodyNormally(HttpMessage &httpMsg);
    void SendBodyInChunk(HttpMessage &httpMsg);

    void GetHeaders(io::Stream &readStream);
    void ParseHeaders(HttpMessage &res);

    virtual void InitDefaultHeaders(HttpMessage &httpMsg);
public:    
    virtual ~HttpEndPoint(void);

    WeakReferenceT<ICancelContext> *GetCancelContext();
    security::cert::X509Certificate *GetPeerCertificate();
};

} // end of namespace http

} // end of namespace net

} // end of namespace cppflib


#endif
